package com.beverages.messages.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.beverages.messages.client.CustomerMessagesClient;
import com.beverages.messages.client.MerchantOrdersClient;
import com.beverages.messages.client.SharedFilesClient;
import com.beverages.messages.entity.RefundReply;
import com.beverages.messages.entity.dto.RefundReplyDTO;
import com.beverages.messages.entity.dto.intermediate.OrdersDTO;
import com.beverages.messages.entity.intermediate.RefundRequest;
import com.beverages.messages.mapper.RefundReplyMapper;
import com.beverages.messages.service.IRefundReplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;

@Service
public class RefundReplyServiceImpl extends ServiceImpl<RefundReplyMapper, RefundReply> implements IRefundReplyService {

    @Autowired
    private MerchantOrdersClient merchantOrdersClient;

    @Autowired
    private SharedFilesClient sharedFilesClient;

    @Autowired
    private CustomerMessagesClient customerMessagesClient;

    @Override
    public RefundReplyDTO convertRefundReplyEntityToRefundReplyDTO(RefundReply refundReply){
        RefundReplyDTO dto = new RefundReplyDTO();

        dto.setReplyId(refundReply.getId());

        dto.setRequestId(refundReply.getRequestId());
        RefundRequest refundRequest = customerMessagesClient.getRefundRequestById(refundReply.getRequestId()).getData();
        dto.setRequestContent(refundRequest.getContent());
        dto.setImageUrl(sharedFilesClient.getFileUrlById(refundRequest.getImageId()).getData());
        dto.setRequestTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(refundRequest.getRequestTime()));

        dto.setOrderNumber(refundRequest.getOrderNumber());
        OrdersDTO oneOrderDTO = merchantOrdersClient.findOneByOrderNumber(refundRequest.getOrderNumber()).getData();
        dto.setMilkteaId(oneOrderDTO.getMilkteaId());
        dto.setMilkteaName(oneOrderDTO.getMilkteaName());
        dto.setFromCustomerId(oneOrderDTO.getCustomerId());
        dto.setFromCustomerName(oneOrderDTO.getCustomerName());
        dto.setFromCustomerPhone(oneOrderDTO.getCustomerPhone());

        dto.setReplyContent(refundReply.getContent());
        dto.setDecision(refundReply.getDecision());
        dto.setAmount(refundReply.getAmount());
        dto.setReplyTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(refundReply.getReplyTime()));

        return dto;
    }
}